import {Injectable} from '@angular/core';
import {AlertController, ToastController, LoadingController, PopoverController} from 'ionic-angular';

@Injectable()
export class CommonUtilService{

  constructor(private alertCtrl: AlertController,
              private toastCtrl: ToastController,
              private loadingCtrl: LoadingController,
              private popoverCtrl: PopoverController){

  }


  /**
   * 提示弹框
   * @param {string} title    标题
   * @param {string} message  需要提示的消息
   * @param {Function} successCallback 确定回调参数
   * @param {Function} dismissCallback?:Function //子页面关闭后触发父页面的回调函数，可以回传参数回来
   * @author yd
   */
  showAlert(attributes:{title:string,
                        message:string,
                        enableBackdropDismiss?:boolean,
                        successCallback?:Function,
                        dismissCallback?:Function}){

    let alert = this.alertCtrl.create({
      title: attributes.title,
      subTitle: attributes.message,
      enableBackdropDismiss:attributes.enableBackdropDismiss,
      buttons: [
        {
          text:"确定",
          handler:()=>{
            if(attributes.successCallback){
              attributes.successCallback();
            }

          }
        }
      ]
    });
    alert.present();

    //关闭子页面后如果需要回传参数给父页面通过dismiss事件，父页面
    alert.onDidDismiss( () => {

      if(attributes.dismissCallback){
        console.log("showAlert dismissCallback");
        attributes.dismissCallback();
      }

    });
    return alert;
  }

  /**
   * 选择弹出框
   * @param {string} title         标题
   * @param {string} message       消息提示内容
   * @param {string} btnCancelTxt  取消按钮名称
   * @param {string} btnOkTxt      确定按钮名称
   * @param {Function} cancelCallback  取消回调函数
   * @param {Function} successCallback 成功回调函数
   * @returns {Promise<any>}
   * @author yd
   */
  showConfirm(attributes:{title:string;
                          message:string;
                          btnCancelTxt:string;
                          btnOkTxt:string;
                          cancelCallback?:Function;
                          successCallback?:Function;
                        }){

    let alert = this.alertCtrl.create({
      title: attributes.title,
      message: attributes.message,
      buttons: [
        {
          text: attributes.btnCancelTxt,
          role: "cancel",
          handler: () => {
            if(attributes.cancelCallback){
              attributes.cancelCallback();
            }
          }
        },
        {
          text: attributes.btnOkTxt,
          handler: () => {
            if(attributes.successCallback){
              attributes.successCallback();
            }

          }
        }
      ]
    });
    alert.present();
    return alert;
  }

  /**
   *
   * @param {string} message              提示的消息
   * @param {number} duration             显示的时间
   * @param {string} position             显示的位置 {"top", "middle", "bottom".}
   * @param {string} cssClass             自定义显示的样式
   * @param {boolean} showCloseButton     是否打开关闭按钮
   * @param {string} closeButtonText      关闭按钮显示的文字
   * @param {boolean} dismissOnPageChange 弹框消失是否跳转到新的页面
   * @param {Function} successCallback    成功回调
   *
   **/
  showToast(attributes:{message:string;
                        duration?:number;
                        position:string;
                        cssClass?:string;
                        showCloseButton?:boolean;
                        closeButtonText?:string;
                        dismissOnPageChange?:boolean;
                        successCallback?:Function;
                      })
  {
    let toast = this.toastCtrl.create({
      message: attributes.message,
      duration: attributes.duration,
      position: attributes.position,
      cssClass: attributes.cssClass,
      showCloseButton:attributes.showCloseButton,
      closeButtonText:attributes.closeButtonText,
      dismissOnPageChange:attributes.dismissOnPageChange
    });

    toast.onDidDismiss(() => {
      if(attributes.successCallback){
        attributes.successCallback();
      }
    });
    toast.present();
    return toast;
  }

  //loading框
  showLoading(attributes: {content?: string;
                           spinner?: string;
                           cssClass?: string;
                           showBackdrop?: boolean;
                           enableBackdropDismiss?: boolean;
                           dismissOnPageChange?: boolean;
                           duration?: number;
                          })
  {
    let loading = this.loadingCtrl.create({
       content: attributes.content,
      spinner: attributes.spinner,
      cssClass: attributes.cssClass,
      showBackdrop: attributes.showBackdrop ? attributes.showBackdrop : false, //控制是否可以点击其他区域消失loading，ionic默认是true
      enableBackdropDismiss: attributes.enableBackdropDismiss,
      dismissOnPageChange: attributes.dismissOnPageChange,
      duration: attributes.duration
    });

    loading.onDidDismiss(() => {
      console.log('Dismissed loading');
    });


    loading.present();

    return loading;
  }

  /**
   * 弹出框，内容可以是页面或者任意内容
   * @param param
   */
  showPopover(attr: {popoverPage: string; //需要弹出的页面名字，不传的话默认'PopoverPage'
                     data?: any; //需要传给 PopoverPage 的参数
                     opts?: any;
                     dismissCallback?:Function;}) //子页面关闭后触发父页面的回调函数，可以回传参数回来
  {
    //let popoverPage = attr.popoverPage ? attr.popoverPage : 'PopoverPage';
    let data = attr.data ? attr.data : {};
    let opts = attr.opts ? attr.opts : {};
    let popover = this.popoverCtrl.create(attr.popoverPage, data, opts);

    //关闭子页面后如果需要回传参数给父页面通过dismiss事件，父页面
    popover.onDidDismiss( (resp) => {

      if(attr.dismissCallback){
        console.log("showPopover dismissCallback");
        attr.dismissCallback(resp);
      }

    });

    popover.present();

    return popover;
  }

  /**
   *  弹出框，弹出单选框菜单，具体参数请参考showConfirm方法中的内容
   * @param {{title: string; inputList: any; cancelCallback?: Function; successCallback?: Function}} attr
   */
  showRadioAlert(attr:{
    title:string,
    inputList:any,
    cancelCallback?:Function;
    successCallback?:Function;}){
    let alert = this.alertCtrl.create();
    alert.setTitle(attr.title);
    attr.inputList.forEach((vInput)=>{
      alert.addInput(vInput);
    })
    alert.addButton({
      text: '取消',
      handler: (data: any) => {
        if(attr.cancelCallback){
          attr.cancelCallback();
        }
      }
    });
    alert.addButton({
      text: '确定',
      handler: (data: any) => {
        if(attr.successCallback){
          attr.successCallback(data);
        }
      }
    });
    alert.present();
  }

}
